package com.wish.banking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish.banking.entity.CfContract;
import com.wish.banking.entity.CwReceiptPlan;
import com.wish.banking.entity.vo.CfContractVO;
import com.wish.banking.entity.vo.DetailCfContractVO;
import com.wish.banking.mapper.CfContractMapper;
import com.wish.banking.service.ICfContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.banking.service.ICwReceiptPlanService;
import com.wish.common.core.utils.StringUtils;
import com.wish.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * ！房态-杨子强 + 崔(租客合约) + 曹佳林申请租房分期 服务实现类
 * </p>
 *
 * @author gaoluwen
 * @since 2022-04-04
 */
@Service
public class CfContractServiceImpl extends ServiceImpl<CfContractMapper, CfContract> implements ICfContractService {

    @Autowired
    private ICwReceiptPlanService receiptPlanService;

    @Override
    public List<CfContract> contractList(CfContract cfContract) {
        return baseMapper.contractList(cfContract);
    }

    @Override
    public IPage<CfContract> searchCfContract(CfContractVO cfContract) {
        Page<CfContract> page = new Page<>(cfContract.getCurrent(), cfContract.getSize());

        //小区租客名称模糊查询
        QueryWrapper<CfContract> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(cfContract.getPremiseName()))
            queryWrapper.like("premise_name",cfContract.getPremiseName());
        if(StringUtils.isNotEmpty(cfContract.getTenantName()))
            queryWrapper.like("tenant_name",cfContract.getTenantName());
        //逻辑删除 否
        queryWrapper.eq("del_flg",0);
        //房租分期 否
        queryWrapper.eq("rent_installment_flg",0);
        //租约保理 否
        queryWrapper.eq("zybl_flg",0);
        //合同状态 已提交
        queryWrapper.eq("contract_state",2);
        //续约状态 未续约
        queryWrapper.eq("xuyue_state",0);
        //冻结状态 未冻结
        queryWrapper.eq("freeze_status",0);
        return getBaseMapper().selectPage(page,queryWrapper);
    }

    //获取合同详情
    @Override
    public DetailCfContractVO detailCfContract(Long cfContractId) {
        //查询合同详细信息
        CfContract cfContract = getBaseMapper().selectById(cfContractId);
        DetailCfContractVO detailCfContractVO = new DetailCfContractVO();
        BeanUtils.copyBeanProp(detailCfContractVO,cfContract);

        //查询收款计划表
        QueryWrapper<CwReceiptPlan> receiptPlanQueryWrapper = new QueryWrapper<>();
        // 合同ID
        receiptPlanQueryWrapper.eq("contract_id",cfContractId);
        // 合同类型：出房
        receiptPlanQueryWrapper.eq("contract_type",2);
        // 费用类型 ：租金
        receiptPlanQueryWrapper.eq("item_type",3);
        List<CwReceiptPlan> receiptPlanList = receiptPlanService.list(receiptPlanQueryWrapper);
        detailCfContractVO.setReceiptList(receiptPlanList);
        return detailCfContractVO;
    }
}
